package com.stepforward.me;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Address;
import android.location.Geocoder;
import android.provider.BaseColumns;
import android.util.Log;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

/** Helper to the database, manages versions and creation */
public class EventDataSQLHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "contact2.db";
    private static final int DATABASE_VERSION = 1;
    // Table name
    public static final String TABLE = "contact2";
    // Columns
    public static final String NAME = "name";
    public static final String ADDRESS = "address";
    public static final String PHONE = "phone";
    public static final String EMAIL = "email";

    public EventDataSQLHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TABLE + "( " + BaseColumns._ID
                + " integer primary key autoincrement, " + NAME + " text not null, "
                + ADDRESS + " text not null, " + PHONE + " text not null, "
              //  + LATITUDE + " text, " + LONGITUDE + " text, "
                + EMAIL + " text not null);";
        Log.d("EventsData", "onCreate: " + sql);
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion >= newVersion) {
            return;
        }

        String sql = null;
        if (oldVersion == 1) {
            sql = "alter table " + TABLE + " add note text;";
        }
        if (oldVersion == 2) {
            sql = "";
        }

        Log.d("EventsData", "onUpgrade	: " + sql);
        if (sql != null) {
            db.execSQL(sql);
        }
    }

    public void update(int rowId, String name, String address, String phone, String email) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues initialValues = new ContentValues();
        initialValues.put(EventDataSQLHelper.NAME, name);
        initialValues.put(EventDataSQLHelper.ADDRESS, address);
        initialValues.put(EventDataSQLHelper.PHONE, phone);
        initialValues.put(EventDataSQLHelper.EMAIL, email);
        db.update(EventDataSQLHelper.TABLE, initialValues,
                BaseColumns._ID + "=" + rowId, null);

    }

  
    public void add(String name, String address, String phone, String email) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(EventDataSQLHelper.NAME, name);
        values.put(EventDataSQLHelper.ADDRESS, address);
        values.put(EventDataSQLHelper.PHONE, phone);
        values.put(EventDataSQLHelper.EMAIL, email);
        db.insert(EventDataSQLHelper.TABLE, null, values);
        db.close();
    }

    public void delete(String id) {
        SQLiteDatabase db = getWritableDatabase();
        db.delete(EventDataSQLHelper.TABLE, BaseColumns._ID + " = ?  ", new String[]{id});
        db.close();
    }
      public Cursor retrieveAll() {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
                null, null);
        return cursor;
    }

      public Cursor retrieveOne(int id) {
        SQLiteDatabase db = getReadableDatabase();
         Cursor cursor = db.query(true, EventDataSQLHelper.TABLE, null,
                BaseColumns._ID + "=" + id, null, null, null, null, null);
        return cursor;
    }
}
